package com.base.producer;

import com.base.entity.OrderEntity;
import com.base.service.ProducerService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private ProducerService producerService;

    @RequestMapping("/sendMsg")
    public String sendMsg() {
        OrderEntity orderEntity = new OrderEntity("123456","ordername");
        rocketMQTemplate.convertAndSend("mytopic", orderEntity);
        return "success";
    }

    @RequestMapping("/sendOrder")
    public String sendOrder() {
        return producerService.saveOrder();
//        OrderEntity orderEntity = new OrderEntity("123456","ordername");
//        rocketMQTemplate.convertAndSend("mytopic", orderEntity);
//        return "success";
    }

    /**************验证rocketmq顺序消费***************/
    @RequestMapping("/send/ordered")
    public String sendOrderedMsg() {
        /**
         * hashKey: 为了保证报到同一个队列中，将消息发送到orderTopic主题上
         */
        rocketMQTemplate.syncSendOrderly("orderTopic", "no1", "order");
        rocketMQTemplate.syncSendOrderly("orderTopic", "no2", "order");
        rocketMQTemplate.syncSendOrderly("orderTopic", "no3", "order");
        rocketMQTemplate.syncSendOrderly("orderTopic", "no4", "order");
        return "success";
        //向 orderTopic主题发送4条消息，内容分别是 no1 no2 no3 no4. 第三个参数是order ,
        // 他的作用是会根据他的hash值计算发送到哪一个队列，我用的是同一个值order,
        // 那么他们的hash一样就可以保证发送到同一个队列里
    }
}
